import re
import threading
import psycopg2
def filter_tag(htmlstr):
    re1 = re.compile('</p>')
    re2=re.compile('<p align.+?>')
    re3=re.compile('<p>')
    re4=re.compile('</br>')
    re5=re.compile('<a target.+?>')
    re6=re.compile('</a>')
    re7=re.compile('&times;&times;')
    s=re1.sub('\n',htmlstr)
    s=re2.sub("",s)
    s=re3.sub("",s)
    s=re4.sub("\n",s)
    s=re5.sub("",s)
    s=re6.sub("",s)
    s=re7.sub("某某",s)
    return s
def insert_json_enter(path):
    index=0
    with open(path,encoding="utf-8")as file:
        lines=file.readlines()
    for line in lines:
        line_splits=line.split("\",  \"")
        cpjg,zw,wsmc,wsmd5,ay,wslx='','','','','',''
        for line_split in line_splits:
            if line_split.count("裁判结果\" : \""):
                cpjg=line_split.replace("裁判结果\" : \"","")
            if line_split.count("正文\" : \""):
                zw=filter_tag(line_split.replace("正文\" : \"",""))
            if line_split.count("文书名称\" : \""):
                wsmc=line_split.replace("文书名称\" : \"","")
            if line_split.count("文书id\" : \""):
                wsmd5=line_split.replace("文书id\" : \"","")
            if line_split.count("案由\" : \""):
                ay=line_split.replace("案由\" : \"","")
            if line_split.count("文书类型\" : \""):
                wslx=line_split.replace("文书类型\" : \"","")
        # print(wsmc)
        if cpjg!=""and zw!=""and wsmc!=""and wsmd5!=""and ay!=""and wslx!="":
            try:
                # print(wslx)
                cur.execute("INSERT INTO ws_json_enter(wsmc,wsid,ay,wslx,cpjg,zw)VALUES('%s','%s','%s','%s','%s','%s');"%(wsmc,wsmd5,ay,wslx,cpjg,zw))
            except:
                print("插入错误")
        index+=1
        print(index)
        conn.commit()
path_list=["./1.json","./2.json","./3.json","./4.json","./5.json","./6.json","./7.json","./8.json","./9.json","./a.json","./b.json","./c.json","./d.json","./e.json","./f.json","./u1.json","./u2.json","./u3.json"]
# path_list=["./1.json"]
threads=[]
files=range(len(path_list))
conn = psycopg2.connect(database='justice', user='beaver', password='123456', host='127.0.0.1', port='5432')  # 127.0.0.1
cur = conn.cursor()
print("链接数据库成功")
for i in files:
    t=threading.Thread(target=insert_json_enter(path_list[i]))
    threads.append(t)
if __name__=="__main__":
    for i in files:
        threads[i].start()
    for i in files:
        threads[i].join()
    print("ALL done!")
    conn.close()